package com.lianxueda.drawing.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lianxueda.drawing.common.R;
import com.lianxueda.drawing.domain.dto.SchedulesFormDTO;
import com.lianxueda.drawing.domain.dto.SchedulesQueryDTO;
import com.lianxueda.drawing.domain.vo.PageVO;
import com.lianxueda.drawing.domain.vo.SchedulesVO;
import com.lianxueda.drawing.domain.pojo.Schedules;
import com.lianxueda.drawing.mapper.SchedulesMapper;
import com.lianxueda.drawing.service.SchedulesService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 29264
 */
@RestController
@RequestMapping("/api/schedules")
@Slf4j
@Tag(name = "课表管理模块")
public class SchedulesController {
    @Resource
    private SchedulesService schedulesService;

    @PostMapping("/save")
    @Operation(summary = "保存课表",description = "保存课表")
    public R save(@RequestBody SchedulesFormDTO schedulesFormDTO) {
        Schedules schedules = new Schedules();
        BeanUtils.copyProperties(schedulesFormDTO, schedules);
        schedulesService.save(schedules);
        return R.suc(schedules);
    }
    @PutMapping("/update")
    @Operation(summary = "修改课表",description = "修改课表")
    public R update(@RequestBody SchedulesFormDTO schedulesFormDTO) {
        Schedules schedules = new Schedules();
        BeanUtils.copyProperties(schedulesFormDTO, schedules);
        schedulesService.updateById(schedules);
        return R.suc(schedules);
    }
    @DeleteMapping("/delete")
    @Operation(summary = "删除课表",description = "删除课表")
    public R delete(@RequestParam(value = "id")Integer id) {
        schedulesService.removeById(id);
        return R.suc(id);
    }
    @PostMapping("/search")
    @Operation(summary = "查询课表", description = "分页查询课表")
    public R search(@RequestBody SchedulesQueryDTO schedulesQueryDTO) {
        IPage<Schedules> page = new Page<>(schedulesQueryDTO.getCurPage(), schedulesQueryDTO.getPageSize());
        LambdaQueryWrapper<Schedules> queryWrapper = new LambdaQueryWrapper<>();

        // 按名称模糊查询
        queryWrapper.like(StringUtils.isNotBlank(schedulesQueryDTO.getName()),
                Schedules::getName, schedulesQueryDTO.getName());

        // 按位置模糊查询
        queryWrapper.like(StringUtils.isNotBlank(schedulesQueryDTO.getLocation()),
                Schedules::getLocation, schedulesQueryDTO.getLocation());

        // 按时间范围查询 (使用字符串进行 datetime 字段的比较)
        if (StringUtils.isNotBlank(schedulesQueryDTO.getStartTime()) && StringUtils.isNotBlank(schedulesQueryDTO.getEndTime())) {
            queryWrapper.between(Schedules::getStartTime, schedulesQueryDTO.getStartTime(), schedulesQueryDTO.getEndTime());
        } else if (StringUtils.isNotBlank(schedulesQueryDTO.getStartTime())) {
            queryWrapper.ge(Schedules::getStartTime, schedulesQueryDTO.getStartTime());
        } else if (StringUtils.isNotBlank(schedulesQueryDTO.getEndTime())) {
            queryWrapper.le(Schedules::getStartTime, schedulesQueryDTO.getEndTime());
        }

        // 排除逻辑删除的数据
        queryWrapper.select(Schedules.class, info -> !info.getColumn().equals("deleted"));

        IPage<Schedules> pageResult = schedulesService.page(page, queryWrapper);

        // 封装分页结果
        PageVO<Schedules> pageVo = new PageVO<>();
        pageVo.setTotal(pageResult.getTotal());
        pageVo.setList(pageResult.getRecords());
        pageVo.setPageSize(pageResult.getSize());
        pageVo.setCurrentPage(pageResult.getCurrent());

        return R.suc(pageVo);
    }

@Resource
private SchedulesMapper schedulesMapper;

    //多用接口，可以删除，新增，修改，接受的是schedules对象数组
    @PostMapping("/saveBanth")
    @Tag(name = "课表的批量操作")
    public R saveSchedules(@RequestBody List<Schedules> schedules) {
        schedulesService.saveOrUpdateSchedules(schedules);
        return R.suc("操作成功");
    }

    //测试mapper层里面的 void deleteAllById(@Param("ids")  List<Integer> ids);这个方法
    @DeleteMapping("/deleteAllById")
    @Tag(name = "课表的批量操作")
    public R deleteAllById(@RequestBody List<Integer> ids) {
        schedulesMapper.deleteAllById(ids);
        return R.suc("操作成功");
    }

}
